/*
 * $Id: ConversionUtilities.java 47 2013-05-30 08:15:40Z gabakyan $
 * $Author: gabakyan $
 * $Revision: 47 $
 * $Date: 2013-05-30 08:15:40 +0000 (Thu, 30 May 2013) $
 *
 * Copyright (c) 2013 Supply Chain Intelligence (SCI), Inc.
 * http://www.scintelligence.com/, Email: info@scintelligence.com
 * All rights reserved.
 *
 * This file is part of Logistics Map.
 *
 * Logistics Map is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * Logistics Map is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Logistics Map.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.sci.logisticsmap.support.converter;

import com.sci.logisticsmap.support.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * The utility class which provides helper methods for conversion.
 *
 * @author gabakyan@gmail.com (Last modified by $Author: gabakyan $)
 * @version $Revision: 47 $
 */
@Component
public class ConversionUtilities {
    /**
     * The conversion service.
     */
	@Autowired
	private ConversionService conversionService;

    /**
     * Converts the source to the target.
     *
     * @param source The conversion source.
     * @param targetType The target type.
     * @return The conversion target.
     */
    public <Source, Target> Target convert(Source source, Class<Target> targetType) {
        return conversionService.convert(source, targetType);
    }

    /**
     * Converts the sources to the targets.
     *
     * @param sources The conversion sources.
     * @param targetType The target type.
     * @return The conversion targets.
     */
    public <Source, Target> List<Target> convert(List<Source> sources, Class<Target> targetType) {
        List<Target> targets = new ArrayList<Target>();
        for (Source source : sources) {
            targets.add(conversionService.convert(source, targetType));
        }
        return targets;
    }

    /**
     * Converts the source page to the target page.
     *
     * @param sourcePage The conversion source page.
     * @param targetType The target type.
     * @return The conversion target page.
     */
    public <Source, Target> Page<Target> convert(Page<Source> sourcePage, Class<Target> targetType) {
        List<Target> targets = new ArrayList<Target>();
        for (Source source : sourcePage/*.getContent()*/) {
            targets.add(conversionService.convert(source, targetType));
        }
        return new Page<Target>(targets, sourcePage.getNumber(), sourcePage.getSize(), sourcePage.getTotal());
    }
}
